import moment from "moment";
import _ from "lodash";
import CModel from "~/src/model";
import API_RES from "~/src/constants/api_res";
import Util from "~/src/library/utils/modules/util";
import DATE_FORMAT from "~/src/constants/date_format";
import RouterConfigBuilder from "~/src/library/utils/modules/router_config_builder";
import MErrorSummary from "~/src/model/summary/error_summary";
import MApiSummary from "~/src/model/summary/api_summary";
import BehaviorDistribution from "~/src/model/parse/behavior_distribution";
import Performance from "~/src/model/parse/performance";
import Monitor from "~/src/model/parse/monitor";
import MonitorExt from "~/src/model/parse/monitor_ext";
import PV from "~/src/model/parse/pv_record";
import UV from "~/src/model/parse/duration_distribution";
import MUserFirstLoginAt from "~/src/model/parse/user_first_login_at";
import MNewUserSummary from "~/src/model/summary/new_user_summary";
import MDevice from "~/src/model/summary/system_device";

let getReportList = RouterConfigBuilder.routerConfigBuilder(
  "/server/report.png",
  RouterConfigBuilder.METHOD_TYPE_POST,

  async (req, res) => {
    const type = _.get(req, ["query", "type"]);
    const query = _.get(req, ["query"]);
    const body = _.isEmpty(_.get(req, ["body"]))
      ? {}
      : JSON.parse(_.get(req, ["body"]));
    if (type == "error") {
      MErrorSummary.insertErrorSummaryRecord(
        query.token,
        query.beginTime / 1000,
        DATE_FORMAT.UNIT.MINUTE,
        query.st,
        MErrorSummary.switchErrorName(query.st).name,
        query.url,
        { 中国: { 四川: { 成都: parseInt(query.times) } } },
        query.times
      );
      MErrorSummary.insertErrorSummaryRecord(
        query.token,
        query.beginTime / 1000,
        DATE_FORMAT.UNIT.HOUR,
        query.st,
        MErrorSummary.switchErrorName(query.st).name,
        query.url,
        { 中国: { 四川: { 成都: parseInt(query.times) } } },
        query.times
      );
      MErrorSummary.insertErrorSummaryRecord(
        query.token,
        query.beginTime / 1000,
        DATE_FORMAT.UNIT.DAY,
        query.st,
        MErrorSummary.switchErrorName(query.st).name,
        query.url,
        { 中国: { 四川: { 成都: parseInt(query.times) } } },
        query.times
      );
      Monitor.insertMonitor(
        query.token,
        query.st,
        MErrorSummary.switchErrorName(query.st).name,
        200,
        moment().unix(),
        10,
        10,
        10,
        query.url,
        "中国",
        "四川",
        "成都",
        query.beginTime / 1000,
        "md5",
        query.beginTime / 1000
      );
      MonitorExt.insertMonitorExt(
        query.token,
        JSON.stringify({ 错误信息: query.msg }),
        moment().unix()
      );
    } else if (type == "res") {
      //  console.log(query,body)
      // MApiSummary.insertApiSummaryRecord(
      //   query.token,
      //   query.beginTime / 1000,
      //   DATE_FORMAT.UNIT.MINUTE,
      //   query.code,
      //   query.time,
      //   query.msg,
      //   query.url,
      //   body.api,
      //   { 中国: { 四川: { 成都: parseInt(query.times) } } }
      // );
    } else if (type == "api") {
      //  console.log(query,body)
      if (query.code != "" && query.code) {
        MApiSummary.insertApiSummaryRecord(
          query.token,
          query.beginTime / 1000,
          DATE_FORMAT.UNIT.DAY,
          query.code,
          query.time,
          query.msg,
          query.url,
          body.api,
          { 中国: { 四川: { 成都: parseInt(query.times) } } }
        );
      }
    } else if (type == "behavior") {
      let name = "";
      switch (body.behavior.type) {
        case "console":
          name = "控制台打印";
          break;
        case "navigation":
          name = "导航切换";
          break;
        case "ui.blur":
          name = "用户失去焦点";
          break;
        case "ui.click":
          name = "用户点击";
          break;
      }
      BehaviorDistribution.insertBehaviorRecord(
        query.token,
        body.behavior.type,
        name,
        query.page,
        query.times,
        query.beginTime / 1000,
        DATE_FORMAT.UNIT.DAY,
        { 中国: { 四川: { 成都: parseInt(query.times) } } }
      );
      // BehaviorDistribution.insertBehaviorRecord(
      //   query.token,
      //   body.behavior.type,
      //   body.behavior.type,
      //   query.url,
      //   query.times,
      //   query.beginTime / 1000,
      //   DATE_FORMAT.UNIT.MINUTE,
      //   { 中国: { 四川: { 成都: parseInt(query.times) } } }
      // );
    } else if (type == "perf") {
      for (let indicator of Performance.INDICATOR_TYPE_LIST) {
        let sum = query[Performance.switchAbbr(indicator)];
        Performance.insertInto(
          query.token,
          query.url,
          indicator,
          query.beginTime / 1000,
          DATE_FORMAT.UNIT.MINUTE,
          sum,
          query.times,
          { 中国: { 四川: { 成都: parseInt(query.times) } } }
        );
        Performance.insertInto(
          query.token,
          query.url,
          indicator,
          query.beginTime / 1000,
          DATE_FORMAT.UNIT.HOUR,
          sum,
          query.times,
          { 中国: { 四川: { 成都: parseInt(query.times) } } }
        );
      }
    } else if (type == "pv") {
      PV.replacePvRecord(
        query.token,
        query.uid,
        query.url,
        query.dt,
        query.dl,
        query.dr,
        query.dpr,
        query.de,
        parseInt(query.times),
        1,
        DATE_FORMAT.UNIT.HOUR,
        parseInt(query.beginTime / 1000)
      );
    } else if (type == "health") {
      // UV.insertUV(
      //   query.token,
      //   query.stay,
      //   query.errcount,
      //   query.beginTime / 1000,
      //   DATE_FORMAT.UNIT.HOUR,
      //   { 中国: { 四川: { 成都: parseInt(query.times) } } }
      // );
      MDevice.replaceAndAutoIncreaseDeviceRecord(
        query.token,
        JSON.parse(query.deviceInfo),
        query.times,
        query.beginTime / 1000,
        { 中国: { 四川: { 成都: parseInt(query.times) } } },
        query.version
      );
      MUserFirstLoginAt.replaceInto(
        query.token,
        query.uid.substring(0, 20),
        query.beginTime / 1000,
        "中国",
        "四川",
        "成都"
      );
      MNewUserSummary.replaceInto(
        query.token,
        1,
        moment
          .unix(query.beginTime / 1000)
          .format(DATE_FORMAT.DATABASE_BY_UNIT[DATE_FORMAT.UNIT.HOUR]),
        DATE_FORMAT.UNIT.HOUR,
        { 中国: { 四川: { 成都: parseInt(query.times) } } }
      );
    }
    res.send(API_RES.showResult(null, "上报云设计监控中心成功！"));
  },
  false,
  false
);

export default {
  ...getReportList,
};
